<?php
/**
 * 菜单管理
 */

class Model_Menu {
	
	/**
	 * 按父ID查找菜单子项
	 *
	 * @param $parentid integer
	 *       	 父菜单ID
	 * @param $with_self integer
	 *       	 是否包括他自己
	 */
	public static function admin_menu($parentid, $with_self = 0) {
		$parentid = intval ( $parentid );
		$menudb = new DB_Menu ();
		$result = $menudb->loadList ( "*", "parentid={$parentid} and display=1", "listorder ASC", 1000 );
		if ($with_self) {
			$result2 [] = $menudb->loadInfoByPrimary ( $parentid );
			$result = array_merge ( $result2, $result );
		}
		return $result;
	}
	
	/**
	 * 添加菜单
	 *
	 * @param $data array       	
	 */
	public function add_menu($data) {
		if (! is_array ( $data )) {
			return false;
		}
		
		$DB_Menu = new DB_Menu ();
		if ($DB_Menu->insert ( $data )) {
			return true;
		}
		return false;
	}
	
	/**
	 * 编辑菜单
	 *
	 * @param $id int       	
	 * @param $data array       	
	 */
	public function edit_menu($id, $data) {
		if (! is_array ( $data ) || empty ( $id )) {
			return false;
		}
		
		$DB_Menu = new DB_Menu ();
		if ($DB_Menu->updateByConditions ( $data, "id={$id}" )) {
			return true;
		}
		return false;
	}
	
	/**
	 * 删除菜单
	 *
	 * @param $id int       	
	 */
	public function delete_menu($id) {
		if (empty ( $id )) {
			return false;
		}
		$DB_Menu = new DB_Menu ();
		if ($DB_Menu->delete ( "id={$id}" )) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * 当前位置
	 *
	 * @param $id int       	
	 */
	public static function current_pos($menuid) {
		$menuid = intval ( $menuid );
		$menudb = new DB_Menu ();
		$return = $menudb->loadInfoByPrimary ( $menuid );
		$str = '';
		if ($return ['parentid']) {
			$str = self::current_pos ( $return ['parentid'] );
		}
		return $str . $return ['name'] . ' > ';
	}
}
?>